
//
//  ReplyLetterViewController.m
//  MJApp
//
//  Created by 王亮 on 15/7/31.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "ReplyLetterViewController.h"
#import "ReplyLetterComeTableViewCell.h"
#import "ReplyLetterSendTableViewCell.h"
@interface ReplyLetterViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSDictionary *originDic;
@property(nonatomic,strong)NSMutableArray *listArray;
@property (weak, nonatomic) IBOutlet UIButton *replyBtn;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ReplyLetterViewController


- (NSMutableArray *)listArray {
    
    if (_listArray == nil) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (instancetype)initWithDic:(NSDictionary *)dic {
    
    if (self = [super init]) {
        
        self.originDic = dic;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self obationData];
    [self UI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)obationData {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"user_id"] = [userInfoTool share].ID;
    dic[@"other_uid"] = self.originDic[@"uid"];
//    dic[@"page"] = @"1";
    [WLHttpRequestAfter userGetLetterForWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            NSArray *array = resultDictionary[@"data"];
            for (int i = (int)array.count - 1; i >= 0; i --) {
                
                [self.listArray addObject:array[i]];
            }
            [self.tableView reloadData];
            CGSize size = self.tableView.contentSize;
            [self.tableView setContentOffset:CGPointMake(0,size.height - CGRectGetHeight(self.tableView.frame))];
        }
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)UI {
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.backgroundColor = Color_Back_Gray;
    self.replyBtn.layer.cornerRadius = 5;
    self.backView.backgroundColor = Color_Back_Gray;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.listArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *contect = self.listArray[indexPath.row][@"content"];
    CGSize size = [contect sizeWithFont:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(250, CGFLOAT_MAX)];
//    NSLog(@"%@",NSStringFromCGSize(size));
    CGFloat height1 = size.height + 20 + 50 ;
    CGFloat height2 = 52 + 40 ;
    return MAX(height1, height2);
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    CGFloat cellHigh = [self tableView:tableView heightForRowAtIndexPath:indexPath];
    
    NSLog(@"uid = %@, from_id = %@,use_id = %@",self.originDic[@"uid"],self.listArray[indexPath.row][@"from_user_id"],[userInfoTool share].ID);
    
    if (![self.originDic[@"uid"] isEqual:self.listArray[indexPath.row][@"from_user_id"]]) {
        
        NSString *cellID = @"ReplyLetterSendTableViewCell";
        ReplyLetterSendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            
            cell = WL_XIB(@"ReplyLetterSendTableViewCell");
        }
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.listArray[indexPath.row]];
        dic[@"cellHigh"] = @(cellHigh);
        cell.infoDic = dic;
        return cell;
    }else {
        
        NSString *cellID = @"ReplyLetterComeTableViewCell";
        ReplyLetterComeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            
            cell = WL_XIB(@"ReplyLetterComeTableViewCell");
        }
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.listArray[indexPath.row]];
        dic[@"cellHigh"] = @(cellHigh);
        cell.infoDic = dic;
        return cell;
    }
}
- (IBAction)replyBtnAction:(id)sender {
    NSString *string = self.textField.text;
    
   string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([string isEqual:@""]) {
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"内容为空"];
    }else {
        
        [self reply];
    }
}

- (void)reply {
    
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        dic1[@"login_user_id"] = [userInfoTool share].ID;
        dic1[@"user_id"] = self.originDic[@"uid"];
        dic1[@"content"] = self.textField.text;
        [WLHttpRequestAfter userReplyLetterWithParam:dic1 usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
            
            
            [self obationData];
            self.textField.text = @"";
            [self.textField resignFirstResponder];
        } andFailureBlock:^(NSError *resultError) {
            
        }];
}

@end
